﻿using System;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using Mimoza.GUI;
using Mimoza.Common;
using Mimoza.ObjectLayer;
using Mimoza.PluginLayer;
using Mimoza.SystemPlugin;
using Mimoza.Plugins.DisplaySettings;
using Mimoza.VNCPlugin;
using Mimoza.RdpPlugin;
using Mimoza.AgentLayer;

namespace SetupWiz
{

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main(string[] args)
        {

#if !WindowsCE
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
#endif

            try
            {
                Mimoza.Common.GlobalSettings.InitLicenceFile();

                if (args.Length == 0)
                {
                    RunStartupWizard();
                }
                else if (args[0] == "Display")
                {
                    RunDisplayRsoulutionDialog();
                }
                else if (args[0] == "VNC")
                {
                    RunVNCSettingsDialog();
                }
                else if (args[0] == "Shortcuts")
                {
                    RunShortcutsDialog();
                }
                else if (args[0] == "RDP")
                {
                    RunNewRDPConnectionDialog();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.StackTrace, Resource.Error_EmptyTerminalName, MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            }
        }

        static string GetDataFromGeristry(string name,string defVal)
        {
            string res = defVal;
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Mimoza", true))
            {
                if (key != null)
                {
                    res = key.GetValue(name, defVal).ToString();
                }
            }

            return res;
        }

        static void RunNewRDPConnectionDialog()
        {
            BaseWizard wizard = new BaseWizard();
            NewRDPConnection page = new NewRDPConnection();
            RdpData data = new RdpData();

            wizard.Size = new System.Drawing.Size(600, 420);
            wizard.Location = new System.Drawing.Point((800 - wizard.Size.Width) / 2, (600 - wizard.Size.Height) / 2);
            wizard.Text = Resource.SetupRDPConnection_Caption;
            wizard.UserData = data;

            wizard.Init(WizardMode.Wizard);            
            wizard.AddPage(page);

            if (wizard.Start() == DialogResult.OK)
            {
                ExecutePluginManager pluginManager = new ExecutePluginManager();

                Policy policy = new Policy(null);
                (wizard.UserData as RdpData).IsPersistentShortcut = false;
                policy.Data = (wizard.UserData as RdpData).Serialize();

                pluginManager.Init();

                IExecutePlugin executeSystemPlugin = pluginManager.Get("Mimoza.RdpPlugin.ExecutePlugin");
                executeSystemPlugin.Add(policy);
            }
        }

        static void RunVNCSettingsDialog()
        {
            string str = "";

            str = GetDataFromGeristry("VNCPlugin", "");

            BaseWizard wizard = new BaseWizard();
            VNCSettingsPage page = new VNCSettingsPage();
            VNCPluginData data = new VNCPluginData();

            if (str != "")
            {
                data = VNCPluginData.Deserialize(str);
            }

            wizard.Size = new System.Drawing.Size(320, 310);
            wizard.Location = new System.Drawing.Point((800 - wizard.Size.Width) / 2, (600 - wizard.Size.Height) / 2);
            wizard.Text = Resource.SetupVNC_Caption;

            wizard.Init(WizardMode.Dialog);
            wizard.UserData = data;
            wizard.AddPage(page);

            if (wizard.Start() == DialogResult.OK)
            {
                ExecutePluginManager pluginManager = new ExecutePluginManager();

                Policy policy = new Policy(null);
                policy.Data = (wizard.UserData as VNCPluginData).Serialize();

                pluginManager.Init();

                IExecutePlugin executeSystemPlugin = pluginManager.Get("Mimoza.VNCPlugin.ExecutePlugin");
                executeSystemPlugin.Add(policy);

                if (DialogResult.OK == MessageBox.Show(Resource.Reboot_Message, Resource.Reboot_Caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    SystemPower.Reboot();
                }
            }
        }

        static void RunDisplayRsoulutionDialog()
        {
            string displayResolution = "800x600x16-60Hz";

            displayResolution = GetDataFromGeristry("DisplayResolution", displayResolution);

            BaseWizard wizard = new BaseWizard();                
            DisplaySettingsConfigPage page = new DisplaySettingsConfigPage();
            DisplayResolution data = new DisplayResolution(displayResolution);
            data.Model = Mimoza.Common.GlobalSettings.Licence.Model;

            wizard.Size = new System.Drawing.Size(360, 370);
            wizard.Location = new System.Drawing.Point((800 - wizard.Size.Width) / 2, (600 - wizard.Size.Height) / 2);
            wizard.Text = Resource.SetupDisplayResolution_Caption;

            wizard.Init(WizardMode.Dialog);
            wizard.UserData = data;
            wizard.AddPage(page);

            if (wizard.Start() == DialogResult.OK)
            {
                ExecutePluginManager pluginManager = new ExecutePluginManager();
                DisplayConfigPlugin displaySettingsPlugin = new DisplayConfigPlugin();
                Policy displaySettingsPolicy = new Policy(null);

                pluginManager.Init();

                displaySettingsPolicy.Data = displaySettingsPlugin.Serialize(wizard.UserData);
                displaySettingsPlugin.OnBeforePolicySave(displaySettingsPolicy);

                IExecutePlugin executeDisplaySettingsPlugin = pluginManager.Get(displaySettingsPolicy.PluginID);
                executeDisplaySettingsPlugin.Add(displaySettingsPolicy);

                if (DialogResult.OK == MessageBox.Show(Resource.Reboot_Message, Resource.Reboot_Caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    SystemPower.Reboot();
                }
            }
        }

        static void RunShortcutsDialog()
        {
            string str;
            str = GetDataFromGeristry("SystemPlugin", "");

            BaseWizard wizard = new BaseWizard();           
     
            ShortcutsPage page = new ShortcutsPage();
            SystemPluginData data = new SystemPluginData();

            if (str != "")
            {
                data = SystemPluginData.Deserialize(str);
            }

            wizard.Size = new System.Drawing.Size(360, 310);
            wizard.Location = new System.Drawing.Point((800 - wizard.Size.Width) / 2, (600 - wizard.Size.Height) / 2);
            wizard.Text = Resource.SetupSystemSettings_Caption;

            wizard.Init(WizardMode.Dialog);
            wizard.UserData = data;
            wizard.AddPage(page);

            if (wizard.Start() == DialogResult.OK)
            {
                ExecutePluginManager pluginManager = new ExecutePluginManager();

                Policy policy = new Policy(null);
                policy.Data = (wizard.UserData as SystemPluginData).Serialize();
                
                pluginManager.Init();

                IExecutePlugin executeSystemPlugin = pluginManager.Get("Mimoza.SystemPlugin.ExecutePlugin");
                executeSystemPlugin.Add(policy);

                if (DialogResult.OK == MessageBox.Show(Resource.Reboot_Message, Resource.Reboot_Caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    SystemPower.Reboot();
                }
            }
        }

        static void RunStartupWizard()
        {
            BaseWizard wizard = new BaseWizard();                
            TerminalName page1 = new TerminalName();
            MimozaServer page2 = new MimozaServer();
            DisplaySettingsConfigPage page3 = new DisplaySettingsConfigPage();
            DisplayResolution data = new DisplayResolution();
            data.Model = Mimoza.Common.GlobalSettings.Licence.Model;
                
            //wizard.Size = new System.Drawing.Size(630, 450);
            wizard.Location = new System.Drawing.Point((800 - wizard.Size.Width) / 2, (600 - wizard.Size.Height) / 2);
            wizard.Text = Resource.StartUpWizard_Caption;
            wizard.Init(WizardMode.Wizard);
            wizard.UserData = data;

            wizard.AddPage(page1);
            wizard.AddPage(page2);
            wizard.AddPage(page3);
                
            if (wizard.Start() == DialogResult.OK)
            {
                ExecutePluginManager pluginManager = new ExecutePluginManager();
                DisplayConfigPlugin displaySettingsPlugin = new DisplayConfigPlugin();
                Policy displaySettingsPolicy = new Policy(null);

                pluginManager.Init();

                displaySettingsPolicy.Data = displaySettingsPlugin.Serialize(wizard.UserData);
                displaySettingsPlugin.OnBeforePolicySave(displaySettingsPolicy);

                IExecutePlugin executeDisplaySettingsPlugin = pluginManager.Get(displaySettingsPolicy.PluginID);
                executeDisplaySettingsPlugin.Add(displaySettingsPolicy);

                IExecutePlugin executeLocalHostPlugin = pluginManager.Get(Mimoza.PluginLayer.ExecutePluginManager.LocalHostPluginId);
                executeLocalHostPlugin.Add(page1.Policy);

                IExecutePlugin executeSystemPlugin = pluginManager.Get("Mimoza.SystemPlugin.ExecutePlugin");
                executeSystemPlugin.Add(page2.Policy);

                using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Mimoza\\Launch", true))
                {
                    if (key != null)
                    {
                        key.DeleteValue("SetupWiz");
                    }
                }

                if (DialogResult.OK == MessageBox.Show(Resource.Reboot_Message, Resource.Reboot_Caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    SystemPower.Reboot();
                }
            }            
        }
    }
}
